package com.guigusuqi.survey.controller;


import com.guigusuqi.app.entity.LoginWxUser;
import com.guigusuqi.app.entity.WxUser;
import com.guigusuqi.commonutils.utils.BelongCalendarUtils;
import com.guigusuqi.commonutils.utils.GetStartTimeAndEndTime;
import com.guigusuqi.commonutils.utils.RedisCache;
import com.guigusuqi.commonutils.vo.Result;
import com.guigusuqi.hy.entity.IntegralAdmin;
import com.guigusuqi.hy.service.IntegralAdminService;
import com.guigusuqi.survey.dto.QuestionnaireParam;
import com.guigusuqi.survey.entity.UserQuestionnaire;
import com.guigusuqi.survey.service.QuestionnaireService;
import com.guigusuqi.survey.service.UserQuestionnaireService;
import com.guigusuqi.survey.utils.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author suqi
 * @since 2022-04-12
 */
@RestController
@RequestMapping("/survey/questionnaire")
@Api(tags = {"wx-问卷管理"})
@Transactional
public class QuestionnaireController
{
    @Resource
    private QuestionnaireService questionnaireService;

    @Resource
    private UserQuestionnaireService userQuestionnaireService;

    @Resource
    private IntegralAdminService integralAdminService;

    @Resource
    private RedisCache redisCache;

    @ApiOperation("查询问卷的问题")
    @GetMapping("/findQuestion")
    public Result findQuestion()
    {
        return questionnaireService.findQuestion();
    }

    @ApiOperation("发布用户的问卷")
    @PostMapping("/publishQuestionnaire")
    @Transactional
    public Result publishQuestionnaire(
            @RequestBody(required = true) List<Map> questionnaireParams) throws IOException
    {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("Authorization");
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_" + token);
        String openid = loginWxUser.getUser().getOpenid();

        // 判断用户是否已经答过该题
//        int count = userQuestionnaireService.selectPublishQuestionnaireCount(openid);
//
//        if (count > 0)// 说明已经答过该题
//        {
//            return Result.fail().message("已答过该问卷的满意度调查！");
//        }

        List<UserQuestionnaire> userQuestionnaires = userQuestionnaireService.selectQuestionnaireByOpenid(openid);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = simpleDateFormat.format(GetStartTimeAndEndTime.getStartTime());
        String endTime = simpleDateFormat.format(GetStartTimeAndEndTime.getEndTime());
        String format = "yyyy-MM-dd HH:mm:ss";
        for (UserQuestionnaire userQuestionnaire : userQuestionnaires)
        {
            Date createTime = userQuestionnaire.getCreateTime();
            boolean b = BelongCalendarUtils.belongCalendar(startTime, endTime, createTime, format);
            if (b)
            {
                return Result.fail().message("该问卷只能一天填一次！");
            }
        }
        //上传答题
        Boolean isSuccess = questionnaireService.publishQuestionnaireByQuestionnaireId(openid,questionnaireParams);

        if(isSuccess)
        {
            //返回用户答的问卷
            List<UserQuestionnaire> userQuestionnaireList = userQuestionnaireService.selectQuestionnaireByOpenid(openid);
            // 添加积分
            Result result = integralAdminService.addIntegral("2", openid);
            return Result.success().message("发布成功,"+result.getMessage()).data("rows",userQuestionnaireList);
        }else
        {
            return Result.success().message("发布失败,没有这个题目");
        }
    }

    @ApiOperation("查询用户答题记录")
    @GetMapping("selectQuestionnaire")
    public Result selectQuestionnaire()
    {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String token = request.getHeader("Authorization");
        LoginWxUser loginWxUser = redisCache.getCacheObject("TOKEN_" + token);
        String openid = loginWxUser.getUser().getOpenid();

        return Result.success().data("rows",userQuestionnaireService.selectQuestionnaireByOpenid(openid));
    }
}

